package com.yzf.smsforward;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import com.yzf.pcsmsforward.R;
import com.yzf.smsforward.utils.Utils;

/**
 * 项目名称：FoldingVerifyCode
 * 类描述：手机号填写对话框类
 * 创建人：Administrator
 * 创建时间：2019年10月1日
 */
public class TelDialog extends Dialog {

    private Context mContext;
    private EditText tel1;
    private EditText tel2;
    private String telNum1 = "";
    private String telNum2 = "";
    private TextView errorMsg;

    TelDialog(@NonNull Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tel_dialog_layout);
        initView();
    }

    private void initView() {
        this.setCanceledOnTouchOutside(false);
        if (com.yzf.smsforward.Constants.isDulCard) {
            this.findViewById(R.id.tel_dialog_sim2).setVisibility(View.VISIBLE);
        }else{
            this.findViewById(R.id.tel_dialog_sim2).setVisibility(View.GONE);
        }
        tel1 = this.findViewById(R.id.dialog_tel1);
        tel2 = this.findViewById(R.id.dialog_tel2);
        errorMsg = this.findViewById(R.id.dialog_error_msg);
        Button btn = this.findViewById(R.id.dialog_btn);
//        String telstr =  Util.getTel(mContext);//手机号码
//        String tel2str =  Util.getTel2Num(mContext);//手机号码
        String telstr = "";
        String tel2str = "";
        try {
            if(TextUtils.isEmpty(com.yzf.smsforward.Constants.simPhoneNo1)){
                if(com.yzf.smsforward.Constants.isSan){
                    telstr = com.yzf.smsforward.Constants.sanPhoneNo1;
                }else{
                    telstr = Utils.getPhoneNoBySerial(mContext, com.yzf.smsforward.Constants.phoneSub1);
                }
            }else{
                telstr = com.yzf.smsforward.Constants.simPhoneNo1;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if(TextUtils.isEmpty(com.yzf.smsforward.Constants.simPhoneNo2)){
                if(com.yzf.smsforward.Constants.isSan){
                    tel2str = com.yzf.smsforward.Constants.sanPhoneNo2;
                }else{
                    tel2str = Utils.getPhoneNoBySerial(mContext, com.yzf.smsforward.Constants.phoneSub2);
                }
            }else{
                tel2str = com.yzf.smsforward.Constants.simPhoneNo2;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!TextUtils.isEmpty(telstr)) {
            tel1.setText(telstr);
        }
        if (!TextUtils.isEmpty(tel2str)) {
            tel2.setText(tel2str);
        }

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                errorMsg.setText("");
                telNum1 = tel1.getText().toString();
                telNum2 = tel2.getText().toString();
                if (telNum1.length() == 11) {
                    Utils.save2SP(mContext, com.yzf.smsforward.Constants.SIMSERIAL_SHARED, com.yzf.smsforward.Constants.phoneSub1, telNum1);
                    com.yzf.smsforward.Constants.numbers.put(com.yzf.smsforward.Constants.TEL1_KEY, telNum1);
                    com.yzf.smsforward.Constants.sanPhoneNo1 = telNum1;
                } else {
                    errorMsg.setText(R.string.null_tel1);
                }
                if (telNum2.length() == 11) {
//                        Util.save2SP(mContext, Constants.TEL_SHARED, Constants.TEL2_KEY, tel2Json.toString());
                    Utils.save2SP(mContext, com.yzf.smsforward.Constants.SIMSERIAL_SHARED, com.yzf.smsforward.Constants.phoneSub2, telNum2);
                    com.yzf.smsforward.Constants.numbers.put(com.yzf.smsforward.Constants.TEL2_KEY, telNum2);
                    com.yzf.smsforward.Constants.sanPhoneNo2 = telNum2;

                } else if(com.yzf.smsforward.Constants.isDulCard) {
                    errorMsg.setText(R.string.null_tel2);
                }
                if(TextUtils.isEmpty(errorMsg.getText())){
                    TelDialog.this.dismiss();
                }
            }
        });
        ImageView close = this.findViewById(R.id.dialog_close_iv);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TelDialog.this.dismiss();
            }
        });
    }


    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { //按下的如果是BACK，同时没有重复
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
